package com.wyl.clinic.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wyl.clinic.common.Result;
import com.wyl.clinic.entity.Doctor;
import com.wyl.clinic.entity.Menu;
import com.wyl.clinic.entity.Patient;
import com.wyl.clinic.entity.User;
import com.wyl.clinic.enums.RoleEnum;
import com.wyl.clinic.model.LoginVo;
import com.wyl.clinic.model.PatientVo;
import com.wyl.clinic.service.DoctorService;
import com.wyl.clinic.service.MenuService;
import com.wyl.clinic.service.PatientService;
import com.wyl.clinic.service.UserService;
import com.wyl.clinic.utils.JwtUtil;
import com.wyl.clinic.utils.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;

import static com.wyl.clinic.enums.RoleEnum.PATIENT;

/**
 * <p>
 * 医生排班表 前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-02-20
 */
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private PatientService patientService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private DoctorService doctorService;

    /**
     *
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo){
        //密码加密
        String passwd = Md5Util.md5Encryp(loginVo.getPassword());
        User user = userService.lambdaQuery().eq(User::getAccount, loginVo.getAccount()).eq(User::getPassword,passwd).one();
        if (ObjectUtil.isEmpty(user)){
            return Result.fail("用户名或密码错误");
        }
        HashMap<String, Object> res = new HashMap();
        String token = JwtUtil.generateToken(user.getId());
        res.put("token", token);
        List<Menu> menuList = menuService.lambdaQuery().like(Menu::getMenuright,user.getRoleId()).list();
        res.put("user",user);
        //通过角色查具体用户信息
        switch (RoleEnum.findByCode(user.getRoleId())){
            case PATIENT:
                Patient patient = patientService.getOne(new LambdaQueryWrapper<Patient>().eq(Patient::getUserId, user.getId()));
                res.put("patient", patient);
                break;
            case DOCTOR:
                Doctor doctor = doctorService.getOne(new LambdaQueryWrapper<Doctor>().eq(Doctor::getUserId, user.getId()));
                res.put("doctor", doctor);
                break;
            default:
                break;
        }
        res.put("menu",menuList);
        return Result.suc(res);
    }

    /**
     * 注册
     * @param patientVo
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody PatientVo patientVo){
        User user = userService.lambdaQuery().eq(User::getAccount,patientVo.getAccount()).one();
        if (user != null){
            return Result.fail("当前账号已存在");
        }
        if (ObjectUtil.isNotEmpty(patientVo.getIdCard())){
            Patient patient = patientService.lambdaQuery().eq(Patient::getIdCard,patientVo.getIdCard()).one();
            if (patient != null){
                return Result.fail("当前身份证已注册");
            }
        }

        //密码加密
        String passwd = Md5Util.md5Encryp(patientVo.getPassword());

        User newUser = new User();
        BeanUtils.copyProperties(patientVo, newUser);
        newUser.setPassword(passwd);
        newUser.setRoleId(PATIENT.getCode());
        userService.save(newUser);

        Patient newPatient = new Patient();
        BeanUtils.copyProperties(patientVo, newPatient);
        newPatient.setUserId(newUser.getId());
        patientService.save(newPatient);
        return Result.suc();
    }



}
